//---------------------------------------------------------------------
// File: BAMDeploymentStep.cs
// 
// Summary: 
//---------------------------------------------------------------------

using System.Data;
using System.Threading;
using System.Xml;
using BizUnit.Extensions.EntLib.Utilities;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
    /// <summary>
	/// EntLibExportDBDataToDataSetStep exports data contained in a list of tables to a Xml file. The export is carried out using the WriteXml capability of the DataSet.
	/// </summary>
	///
	/// <remarks>
	/// <code escaped="true">
	/// <TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.ExportDBTableDataToDatasetStep">
	///		<DelayBeforeExecution>0</DelayBeforeExecution> <!-- Optional, seconds to delay for this step to complete -->
    ///		<ConfigFileLocation>..\\..\\db.config</ConnectionString>
    ///		<DatabaseConfigName>Northwind</DatabaseConfigName>
	///		<DatasetWriteXmlSchemaPath>C:\DBDtableSchema.xml</DatasetWriteXmlSchemaPath>
	///		<DatasetWriteXmlPath>C:\DBDtable.xml</DatasetWriteXmlPath>
	///		<TableNames>Orders,OrderDetails</TableNames>
	/// </TestStep>
	/// </code>
	/// 
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>DelayBeforeExecution</term>
	///			<description>The number of seconds to wait before executing the step</description>
	///		</item>
    ///		<item>
    ///			<term>ConfigFileLocation</term>
    ///			<description>Location of the Database configuration file</description>
    ///		</item>
    ///     <item>
    ///			<term>DatabaseConfigName</term>
    ///			<description>Name of the Database to be used in the configuration file</description>
    ///		</item>
	///		<item>
	///			<term>DatasetWriteXmlSchemaPath</term>
	///			<description>File location where the Xml schema represenation of the DataSet should be saved</description>
	///		</item>
	///		<item>
	///			<term>DatasetWriteXmlPath</term>
	///			<description>File location where the Xml representation of the DataSet data should be saved</description>
	///		</item>
	///		<item>
	///			<term>TableNames</term>
	///			<description>Comma-separated list of tablenames that should be exported in the DataSet</description>
	///		</item>
	///	</list>
	/// </remarks>	
	public class EntLibExportDBDataToDataSetStep: ITestStep
	{
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
		{
			int delayBeforeCheck = context.ReadConfigAsInt32( testConfig, "DelayBeforeExecution" );
            string configFileLocation = context.ReadConfigAsString(testConfig, "ConfigFileLocation");
            string databaseConfigName = context.ReadConfigAsString(testConfig, "DatabaseConfigName");
            
			string DatasetWriteXmlSchemaPath = context.ReadConfigAsString(testConfig, "DatasetWriteXmlSchemaPath");
			string DatasetWriteXmlPath = context.ReadConfigAsString(testConfig, "DatasetWriteXmlPath");
			string TableNames = context.ReadConfigAsString(testConfig, "TableNames");

			// Sleep for delay seconds...
			Thread.Sleep(delayBeforeCheck);

			DataSet ds = EntLibDatabaseHelper.GetDataSetForExport(TableNames, configFileLocation, databaseConfigName);

			ds.WriteXml(DatasetWriteXmlPath);
			ds.WriteXmlSchema(DatasetWriteXmlSchemaPath);
		}
	}
}
